package mm.chenchen.modules.script.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mm.chenchen.modules.script.dao.model.request.ScriptInstanceQuery;
import mm.chenchen.modules.script.entity.ScriptInstance;
import mm.chenchen.modules.script.service.ScriptInstanceService;
import mm.chenchen.modules.sys.controller.AbstractController;
import mm.chenchen.modules.sys.entity.SysUserEntity;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.R;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 
 *
 * @author 
 * @email 
 * @date 2021-07-29 10:50:49
 */
@RestController
@Api(tags = "实例管理")
@RequestMapping("reptile/scriptinstance")
@CrossOrigin
public class ScriptInstanceController extends AbstractController {
    @Autowired
    private ScriptInstanceService service;

    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    //@RequiresPermissions("reptile:scriptpythoninstance:list")
    public R findPage(@RequestBody ScriptInstanceQuery param){
        return service.findPage(PageUtils.getPage(param), param);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythoninstance:info")
    public R getById(@ApiParam(name = "objId", value = "") @PathVariable Long objId){
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythoninstance:save")
    public R save(@RequestBody ScriptInstance param) throws Exception{
        param.setOperId(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        return service.saveInfo(param);
    }
    @ApiOperation(value = "获取配置文件", notes = "")
    @GetMapping(value = "/config/{objId}", produces = "application/json")
    public R getConfig(@ApiParam(name = "objId", value = "") @PathVariable Long objId){
        return service.getConfig(objId);
    }
    @ApiOperation(value = "提交配置文件", notes = "")
    @PostMapping(value = "/config/{objId}", produces = "application/json")
    public R updateConfig(@ApiParam(name = "objId", value = "") @PathVariable Long objId,@RequestBody Map param){
        return service.updateConfig(objId,param);
    };

    @ApiOperation(value = "运行实例", notes = "")
    @PostMapping(value = "/runInstance/{objId}", produces = "application/json")
    public R runInstance(@ApiParam(name = "objId", value = "") @PathVariable Long objId){
        Long userId=((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId();

        return service.runInstance(objId,userId);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythoninstance:update")
    public R update(@ApiParam(name = "objId", value = "") @PathVariable Long objId,@RequestBody ScriptInstance param){
        param.setId(objId);
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:scriptpythoninstance:delete")
    public R deleteById(@ApiParam(name = "objId", value = "") @PathVariable Long objId){
        return service.deleteById(objId);
    }

}
